package com.novelastudios.utils
{
	import org.casalib.util.StringUtil;
	
	/**
	 * Requires Casalib's StringUtil
	 *  
	 * @author johnbailey ©2009
	 * 
	 */	
	public class StringUtil
	{
		/**
		 * Explanation needed
		 * 
		 * @example This is an over simplified example
		 * <listing version="3.0">
		 * 	var string:String = "Donec porttitor ligula eu dolor.";
		 *  var results:Array = StringUtil.getBetween(string,"lingula","dolor");
		 * 	for( var i:String in results )
		 *  {
		 * 		trace(results[i]);
		 *  }
		 * 	//traces = " eu "
		 * </listing>
		 * 
		 * @param lookin
		 * @param begin
		 * @param end
		 * 	
		 * 
		 * @return 
		 * 
		 */		
		public static function getBetween(lookin:String,begin:String,end:String):Array
		{
			if(!isViable(lookin))
				return [];
			
			var regexp:RegExp = new RegExp("(?<="+begin+").*?(?="+end+")","g");
			var match:Array = lookin.match(regexp);
			return match;
		}
		
		/**
		 * @example Often when you're working with external data there are times when you don't want
		 * 	to use a blank string. This is helpful then to determine if it's viable.
		 * 
		 * <listing version="3.0">
		 * 	var string:String = xml.user[1].username;
		 * 	trace(StringUtil.isViable(string));
		 * </listing>
		 * 
		 * @param value		The string to test
		 * @param strict
		 * @return 
		 * 
		 */		
		public static function isViable(value:String,strict:Boolean=true):Boolean
		{
			if(!value) return false;			
			if(strict) value = org.casalib.util.StringUtil.removeWhitespace(value);
			if(strict && value == "undefined") return false; 
			if(value.length <= 0) return false;
			
			return true;
		}
	}
}